package com.alibaba.cloud.ai.example.manus.schedule.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

public abstract class BaseJob implements Job {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected ApplicationContext applicationContext;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String jobName = context.getJobDetail().getKey().getName();
        String jobGroup = context.getJobDetail().getKey().getGroup();

        logger.info("开始执行定时任务: {}.{}", jobGroup, jobName);

        try {
            long startTime = System.currentTimeMillis();

            // 执行具体任务
            doExecute(context);

            long endTime = System.currentTimeMillis();
            logger.info("定时任务执行完成: {}.{}, 耗时: {}ms", jobGroup, jobName, (endTime - startTime));

        } catch (Exception e) {
            logger.error("定时任务执行失败: {}.{}", jobGroup, jobName, e);
            throw new JobExecutionException(e);
        }
    }

    /**
     * 子类实现具体的任务逻辑
     */
    protected abstract void doExecute(JobExecutionContext context) throws Exception;
}
